<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="Mybatisjar包依赖1234567891011121314151617181920&lt;!-- https:&#x2F;&#x2F;mvnrepository.com&#x2F;artifact&#x2F;org.mybatis&#x2F;mybatis --&gt;   &lt;dependency&gt;     &lt;groupId&gt;org.mybatis&lt;&#x2F;groupId&gt;     &lt;artifactId">
<meta property="og:type" content="article">
<meta property="og:title" content="Mybatis">
<meta property="og:url" content="http://gushihu1.com/2020/11/10/Mybatis/index.html">
<meta property="og:site_name" content="孤失狐的博客">
<meta property="og:description" content="Mybatisjar包依赖1234567891011121314151617181920&lt;!-- https:&#x2F;&#x2F;mvnrepository.com&#x2F;artifact&#x2F;org.mybatis&#x2F;mybatis --&gt;   &lt;dependency&gt;     &lt;groupId&gt;org.mybatis&lt;&#x2F;groupId&gt;     &lt;artifactId">
<meta property="article:published_time" content="2020-11-10T02:03:24.559Z">
<meta property="article:modified_time" content="2020-02-29T13:24:00.774Z">
<meta property="article:author" content="孤失狐">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://gushihu1.com/2020/11/10/Mybatis/"/>





  <title>Mybatis | 孤失狐的博客</title>
  








  <script type="text/javascript" src="//libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  <!-- 雪花特效 -->
  <script type="text/javascript">
  var windowWidth = $(window).width();
  if (windowWidth > 480) {
    document.write('<script type="text/javascript" src="/js/src/snow.js"><\/script>');
  }
  </script>

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">孤失狐的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">勿忘初心</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://gushihu1.com/2020/11/10/Mybatis/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="孤失狐">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/ChatHead.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="孤失狐的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Mybatis</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-11-10T10:03:24+08:00">
                2020-11-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Mybatis"><a href="#Mybatis" class="headerlink" title="Mybatis"></a>Mybatis</h1><h3 id="jar包依赖"><a href="#jar包依赖" class="headerlink" title="jar包依赖"></a>jar包依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.5.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">   <span class="comment">&lt;!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">version</span>&gt;</span>8.0.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">   <span class="comment">&lt;!-- https://mvnrepository.com/artifact/log4j/log4j --&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.17<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="配置mapper文件夹为配置文件夹"><a href="#配置mapper文件夹为配置文件夹" class="headerlink" title="配置mapper文件夹为配置文件夹"></a>配置mapper文件夹为配置文件夹</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resources</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/Java<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">excludes</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">exclude</span>&gt;</span>**/*.java<span class="tag">&lt;/<span class="name">exclude</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;/<span class="name">excludes</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">resource</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">directory</span>&gt;</span>src/main/resources<span class="tag">&lt;/<span class="name">directory</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">includes</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">include</span>&gt;</span>**/*.* <span class="tag">&lt;/<span class="name">include</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;/<span class="name">includes</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;/<span class="name">resource</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">resources</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h3><h4 id="主配置文件"><a href="#主配置文件" class="headerlink" title="主配置文件"></a>主配置文件</h4><ul>
<li>只有一个</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"org/mybatis/example/BlogMapper.xml"</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="mapper配置文件"><a href="#mapper配置文件" class="headerlink" title="mapper配置文件"></a>mapper配置文件</h4><ul>
<li>几个表就有几份</li>
<li>用来写SQL语句</li>
<li></li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driver&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"com/lmt/mapper/StudentMapper.xml"</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="log4j配置信息"><a href="#log4j配置信息" class="headerlink" title="log4j配置信息"></a>log4j配置信息</h4><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">log4j.rootLogger</span>=<span class="string">DEBUG,stdout</span></span><br><span class="line"></span><br><span class="line"><span class="meta">log4j.logger.org.mybatis.example.BlogMapper</span>=<span class="string">TRACE</span></span><br><span class="line"></span><br><span class="line"><span class="meta">log4j.appender.stdout</span>=<span class="string">org.apache.log4j.ConsoleAppender</span></span><br><span class="line"><span class="meta">log4j.appender.stdout.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="meta">log4j.appender.R.layout.ConversionPattern</span>=<span class="string">%d&#123;yyyy-MM-dd HH:mm:ss,SSS&#125; [%t] [%c] [%p] - %m%n</span></span><br></pre></td></tr></table></figure>

<h3 id="连接"><a href="#连接" class="headerlink" title="连接"></a>连接</h3><h4 id="创建"><a href="#创建" class="headerlink" title="创建"></a>创建</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">String resource = <span class="string">"mybatis-config.xml"</span>;</span><br><span class="line">      InputStream inputStream = Resources.getResourceAsStream(resource);</span><br><span class="line">      <span class="comment">//SqlSessionFactory对象的作用就是为我们创建一个SqlSession对象</span></span><br><span class="line">      SqlSessionFactory sqlSessionFactory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</span><br><span class="line"></span><br><span class="line"><span class="comment">//我们未来所有的操作，使用的都是SqlSession对象session来完成</span></span><br><span class="line">      <span class="comment">//例如增删改查，处理事务等等，都是统一使用session对象来完成</span></span><br><span class="line">      SqlSession session = sqlSessionFactory.openSession();</span><br></pre></td></tr></table></figure>

<h4 id="关闭"><a href="#关闭" class="headerlink" title="关闭"></a>关闭</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">session.close();</span><br></pre></td></tr></table></figure>

<h3 id="操作"><a href="#操作" class="headerlink" title="操作"></a>操作</h3><h4 id="查单条"><a href="#查单条" class="headerlink" title="查单条"></a>查单条</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">      *   需求：根据id查单条</span></span><br><span class="line"><span class="comment">      *       如果取得的是单条纪录，我们调用selectOne方法</span></span><br><span class="line"><span class="comment">      *       参数1：根据  命名空间名.SQLID 的形式找到我们需要使用的SQL语句</span></span><br><span class="line"><span class="comment">      *       参数2：我们要为SQL语句中传递的参数</span></span><br><span class="line"><span class="comment">      * */</span></span><br><span class="line">      Student student = session.selectOne(<span class="string">"test1.getById"</span>, <span class="string">"A0001"</span>);</span><br><span class="line">      System.out.println(student);</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--SQL语句必须写在相对应的标签中</span></span><br><span class="line"><span class="comment">       &lt;insert&gt;    &lt;update&gt;    &lt;delete&gt;  &lt;select&gt;</span></span><br><span class="line"><span class="comment">   --&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getById"</span> <span class="attr">parameterType</span>=<span class="string">"java.lang.String"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_stident where id=#&#123;id&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="查所有"><a href="#查所有" class="headerlink" title="查所有"></a>查所有</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查询学生信息表中所有的记录</span></span><br><span class="line">      List&lt;Student&gt; studentList = session.selectList(<span class="string">"test1.getAll"</span>);</span><br><span class="line">      <span class="keyword">for</span> (Student student:studentList)&#123;</span><br><span class="line">          System.out.println(student);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">       如果返回的是多条记录，那么resultType返回值类型，应该写集合的泛型</span></span><br><span class="line"><span class="comment">   --&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getAll"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="添加"><a href="#添加" class="headerlink" title="添加"></a>添加</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">     *   Mybatis默认情况下是手动提交事务</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">     <span class="comment">//添加操作</span></span><br><span class="line">     Student student = <span class="keyword">new</span> Student();</span><br><span class="line">     student.setId(<span class="string">"A0006"</span>);</span><br><span class="line">     student.setName(<span class="string">"cxk"</span>);</span><br><span class="line">     student.setAge(<span class="number">23</span>);</span><br><span class="line">     session.insert(<span class="string">"test1.save"</span>, student);</span><br><span class="line">     session.commit();</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"save"</span>&gt;</span></span><br><span class="line">      insert into tbl_student (id,name,age) values (#&#123;id&#125;,#&#123;name&#125;,#&#123;age&#125;)</span><br><span class="line">  <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//修改操作</span></span><br><span class="line">Student student = <span class="keyword">new</span> Student();</span><br><span class="line">student.setId(<span class="string">"A0006"</span>);</span><br><span class="line">student.setName(<span class="string">"cxk"</span>);</span><br><span class="line">student.setAge(<span class="number">24</span>);</span><br><span class="line">session.update(<span class="string">"test1.update"</span>, student);</span><br><span class="line">session.commit();</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">"update"</span>&gt;</span></span><br><span class="line">       update tbl_student set name=#&#123;name&#125;,age=#&#123;age&#125; where id=#&#123;id&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//删除操作</span></span><br><span class="line">      session.delete(<span class="string">"test1.delete"</span>, <span class="string">"A0006"</span>);</span><br><span class="line">      session.commit();</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"delete"</span>&gt;</span></span><br><span class="line">       delete from tbl_student where id=#&#123;id&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">delete</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="Mybatis与JDBC"><a href="#Mybatis与JDBC" class="headerlink" title="Mybatis与JDBC"></a>Mybatis与JDBC</h3><ol>
<li>获取连接    得到statement    处理rs    关闭资源非常繁琐<ul>
<li>解决：使用SqlSession解决一切</li>
</ul>
</li>
<li>将SQL语句写死到Java代码中，如果修改SQL语句，必须要修改Java代码，需要重新编译。程序可维护性不高<ul>
<li>解决：将SQL语句配置在Mapper.xml文件中与Java代码分离</li>
</ul>
</li>
<li>向PreparedStatement对占位符的位置设置参数时，非常繁琐<ul>
<li>解决：Mybatis自动将Java对象映射至SQL语句，通过statement中的paramentType定义输入参数的类型</li>
</ul>
</li>
<li>解析结果集时需要把字段的值设置到相应的实体类属性名中<ul>
<li>解决：Mybatis自动将SQL执行结果映射至Java对象，通过statement中的resultType定义输出结果的类型</li>
</ul>
</li>
</ol>
<h3 id="Util工具类"><a href="#Util工具类" class="headerlink" title="Util工具类"></a>Util工具类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SqlSessionUtil</span> </span>&#123;</span><br><span class="line">    <span class="comment">//类私有化，不能创建</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="title">SqlSessionUtil</span><span class="params">()</span></span>&#123;&#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> SqlSessionFactory sqlSessionFactory;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        String resource = <span class="string">"mybatis-config.xml"</span>;</span><br><span class="line">        InputStream inputStream = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            inputStream = Resources.getResourceAsStream(resource);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        sqlSessionFactory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> ThreadLocal&lt;SqlSession&gt; t = <span class="keyword">new</span> ThreadLocal&lt;SqlSession&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//取得SqlSession对象</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SqlSession <span class="title">getSession</span><span class="params">()</span></span>&#123;</span><br><span class="line">        SqlSession session = t.get();</span><br><span class="line">        <span class="keyword">if</span> (session==<span class="keyword">null</span>)&#123;</span><br><span class="line">            session = sqlSessionFactory.openSession();</span><br><span class="line">            t.set(session);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> session;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//关闭SqlSession对象</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">myClose</span><span class="params">(SqlSession session)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (session != <span class="keyword">null</span>)&#123;</span><br><span class="line">            session.close();</span><br><span class="line"></span><br><span class="line">            <span class="comment">//这句必须加，非常容易忘</span></span><br><span class="line">            <span class="comment">//清空</span></span><br><span class="line">            t.remove();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="业务层动态代理类"><a href="#业务层动态代理类" class="headerlink" title="业务层动态代理类"></a>业务层动态代理类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TransactionInvocationHandler</span> <span class="keyword">implements</span> <span class="title">InvocationHandler</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Object target;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">TransactionInvocationHandler</span><span class="params">(Object target)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.target = target;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, Method method, Object[] args)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">        SqlSession session = <span class="keyword">null</span>;</span><br><span class="line">        Object obj = <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            session = SqlSessionUtil.getSession();</span><br><span class="line">            <span class="comment">//处理业务逻辑</span></span><br><span class="line">            obj = method.invoke(target, args);</span><br><span class="line">            <span class="comment">//处理业务逻辑完毕后，提交事务</span></span><br><span class="line">            session.commit();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            session.rollback();</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            SqlSessionUtil.myClose(session);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> obj;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">getProxy</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">//取得代理类对象</span></span><br><span class="line">        <span class="keyword">return</span> Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), <span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ServiceFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Object <span class="title">getService</span><span class="params">(Object service)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> TransactionInvocationHandler(service).getProxy();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="通过业务层实现"><a href="#通过业务层实现" class="headerlink" title="通过业务层实现"></a>通过业务层实现</h3><p>dao层</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentDaoImpl</span> <span class="keyword">implements</span> <span class="title">StudentDao</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Student <span class="title">getById</span><span class="params">(String id)</span> </span>&#123;</span><br><span class="line">        SqlSession session = SqlSessionUtil.getSession();</span><br><span class="line">        Student student = session.selectOne(<span class="string">"test1.getById"</span>,id);</span><br><span class="line">        <span class="keyword">return</span> student;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">(Student student)</span> </span>&#123;</span><br><span class="line">        SqlSession session = SqlSessionUtil.getSession();</span><br><span class="line">        session.insert(<span class="string">"test1.save"</span>,student);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>service层</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StudentServiceImpl</span> <span class="keyword">implements</span> <span class="title">StudentService</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> StudentDao studentDao = <span class="keyword">new</span> StudentDaoImpl();</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Student <span class="title">getById</span><span class="params">(String id)</span> </span>&#123;</span><br><span class="line">        Student student = studentDao.getById(id);</span><br><span class="line">        <span class="keyword">return</span> student;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">(Student student)</span> </span>&#123;</span><br><span class="line">        studentDao.save(student);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>主方法调用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">       <span class="comment">// StudentService studentService = new StudentServiceImpl();不行</span></span><br><span class="line">       StudentService studentService = (StudentService) ServiceFactory.getService(<span class="keyword">new</span> StudentServiceImpl());</span><br><span class="line">       Student student = <span class="keyword">new</span> Student();</span><br><span class="line">       student.setId(<span class="string">"A0006"</span>);</span><br><span class="line">       student.setName(<span class="string">"cxk"</span>);</span><br><span class="line">       student.setAge(<span class="number">23</span>);</span><br><span class="line"></span><br><span class="line">       studentService.save(student);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//测试根据id查单条操作</span></span><br><span class="line">      Student student = studentService.getById(<span class="string">"A0001"</span>);</span><br><span class="line">      System.out.println(student);</span><br></pre></td></tr></table></figure>

<h4 id="Mybatis对dao层动态代理的支持"><a href="#Mybatis对dao层动态代理的支持" class="headerlink" title="Mybatis对dao层动态代理的支持"></a>Mybatis对dao层动态代理的支持</h4><ul>
<li><p>我们以前用过动态代理，我们是在业务层使用的，在业务层使用动态代理是为了实现事务管理。</p>
</li>
<li><p>业务层的动态代理是我们手写的</p>
</li>
<li><p>业务层之所以要使用动态代理，是因为业务层本身就是用来处理业务逻辑的，事务相关的代码不方便放在业务层处理，所以我们想到使用代理类帮业务层去处理。</p>
</li>
<li><p>现在我们要在dao层也要加入动态代理。dao层之所以创建代理类，是因为写dao层本身就是一种不方便。在结合了mybatis的动态代理机制后，以后的实际项目开发，dao层的impl就不写了。Mybatis的动态代理不用我们自己手写，在mybatis中已经集成好的一种机制，我们直接拿来使用就可以了。</p>
</li>
</ul>
<h4 id="规定"><a href="#规定" class="headerlink" title="规定"></a>规定</h4><ul>
<li>映射文件名必须和dao接口名在必须相同</li>
<li>映射文件和接口必须在同一个包下</li>
<li>映射文件namespace必须为接口的全路径</li>
<li>接口的方法名必须和映射文件的id相同</li>
<li>对于查询来说，映射文件的传递类型和返回值类型必须和方法的保持一致</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.lmt.dao.StudentDao"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getById"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">        select * from tbl_student where id=#&#123;id&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"save"</span>&gt;</span></span><br><span class="line">        insert into tbl_student(id,name,age)values (#&#123;id&#125;,#&#123;name&#125;,#&#123;age&#125;)</span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">StudentDao</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Student <span class="title">getById</span><span class="params">(String id)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">(Student student)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>对于业务层创建实现类方法为</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> StudentDao studentDao = SqlSessionUtil.getSession().getMapper(StudentDao<span class="class">.<span class="keyword">class</span>)</span>;</span><br></pre></td></tr></table></figure>

<h3 id="增删改查"><a href="#增删改查" class="headerlink" title="增删改查"></a>增删改查</h3><h4 id="查询所有"><a href="#查询所有" class="headerlink" title="查询所有"></a>查询所有</h4><p>主方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//查询所有记录</span></span><br><span class="line">      StudentService studentService = (StudentService) ServiceFactory.getService(<span class="keyword">new</span> StudentServiceImpl());</span><br><span class="line">      List&lt;Student&gt; studentList = studentService.getAll();</span><br><span class="line">      <span class="keyword">for</span> (Student s:studentList)&#123;</span><br><span class="line">          System.out.println(s);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<p>dao service 接口添加对应方法</p>
<p>serviceimpl实现类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> List&lt;Student&gt; <span class="title">getAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       List&lt;Student&gt; studentList = studentDao.getAll();</span><br><span class="line">       <span class="keyword">return</span> studentList;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>dao映射文件</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getAll"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">      select * from tbl_student</span><br><span class="line">  <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="配置文件配置"><a href="#配置文件配置" class="headerlink" title="配置文件配置"></a>配置文件配置</h3><h4 id="主配置文件-1"><a href="#主配置文件-1" class="headerlink" title="主配置文件"></a>主配置文件</h4><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">url</span>=<span class="string">jdbc:mysql://localhost/testmybatis?serverTimezone=UTC</span></span><br><span class="line"><span class="attr">username</span>=<span class="string">root</span></span><br><span class="line"><span class="attr">password</span>=<span class="string">admin</span></span><br><span class="line"><span class="attr">driverClassName</span>=<span class="string">com.mysql.cj.jdbc.Driver</span></span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">        <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta">        <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"db.properties"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--可以搭建环境，通过default进行选择--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"$&#123;driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;url&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;username&#125;"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;password&#125;"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dataSource</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">environment</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">environments</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"com/lmt/dao/StudentDao.xml"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--1--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span><span class="comment">&lt;!--没有必要--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--2--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment"> 		为指定的类分别起别名</span></span><br><span class="line"><span class="comment">		type：要为哪个类起别名，填写全名</span></span><br><span class="line"><span class="comment">		alias：别名的名字</span></span><br><span class="line"><span class="comment">	--&gt;</span></span><br><span class="line">	    <span class="tag">&lt;<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"com.lmt.domain.Student"</span> <span class="attr">alias</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment"> 		为package标签批量起别名</span></span><br><span class="line"><span class="comment">		别名是MyBatis默认为我们取好的，命名不是由我们自己决定，别名为类名（类名的字母不区分大小写）虽然不区分大小写，但是我们还是要按照约定俗称的规则写，写类名或者首字母小写</span></span><br><span class="line"><span class="comment">		name：指定一个包结构，表示在该包下，所有的domain自动起好了别名</span></span><br><span class="line"><span class="comment">	--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.lmt.domain"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">		一般使用批量起别名的方式</span></span><br><span class="line"><span class="comment">		也有公司会摒弃使用起别名的机制，认为全路径可以增加可读性</span></span><br><span class="line"><span class="comment">	--&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--3--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mappers</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">            使用resource属性，指定mapper映射文件</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"com/lmt/dao/StudentDao.xml"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">             使用class属性，找到dao层接口的全路径</span></span><br><span class="line"><span class="comment">         --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.lmt.dao.StudentDao"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">            批量注册(一般使用)</span></span><br><span class="line"><span class="comment">            name属性：指向dao层的包，表示在该dao包下，所有的mapper映射文件自动注册</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.lmt.dao"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">mappers</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="海量级别的数据，提高效率"><a href="#海量级别的数据，提高效率" class="headerlink" title="海量级别的数据，提高效率"></a>海量级别的数据，提高效率</h3><h4 id="基础操作"><a href="#基础操作" class="headerlink" title="基础操作"></a>基础操作</h4><p>对于常用的查询条件的字段，设置索引</p>
<h4 id="高级操作"><a href="#高级操作" class="headerlink" title="高级操作"></a>高级操作</h4><p>使用nosql数据库，redis</p>
<h4 id="专业操作"><a href="#专业操作" class="headerlink" title="专业操作"></a>专业操作</h4><p>搜索引擎（Elasticsearch与Solr），特针对于电商行业</p>
<h3 id="简单类型查询"><a href="#简单类型查询" class="headerlink" title="简单类型查询"></a>简单类型查询</h3><h4 id="String"><a href="#String" class="headerlink" title="String"></a>String</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//1.测试：parameterType 使用简单数据类型 String</span></span><br><span class="line">Student student = studentDao.select1(<span class="string">"A0001"</span>);</span><br><span class="line">System.out.println(student);</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select1"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student where id=#&#123;id&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="int"><a href="#int" class="headerlink" title="int"></a>int</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//2.测试：使用简单数据类型 int</span></span><br><span class="line">      List&lt;Student&gt; student =  studentDao.select2(<span class="number">23</span>);</span><br><span class="line">      <span class="keyword">for</span> (Student student1:student)&#123;</span><br><span class="line">          System.out.println(student1);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select2"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student where age=#&#123;age&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="引用类型和集合类型"><a href="#引用类型和集合类型" class="headerlink" title="引用类型和集合类型"></a>引用类型和集合类型</h3><ul>
<li>在实际项目的开发中，使用domain和map都可以为SQL语句同时传递多个参数</li>
<li>一般情况下，我们使用domain就行了</li>
<li>当domain不符合需求的情况下，我们一定要考虑使用map来传值</li>
</ul>
<h4 id="domain"><a href="#domain" class="headerlink" title="domain"></a>domain</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//3.测试：使用domain为参数</span></span><br><span class="line">     <span class="comment">/* 查询出姓名为wyf,年龄为23岁的学员信息</span></span><br><span class="line"><span class="comment">      绝对不可以为SQL语句传递多个参数,如  select（"wyf",23）</span></span><br><span class="line"><span class="comment">      如果我们要为SQL语句传递多个参数，我们应该将这多个参数封装到一个domain对象中，或者打包到一个map集合中*/</span></span><br><span class="line">      Student student = <span class="keyword">new</span> Student();</span><br><span class="line">      student.setName(<span class="string">"wyf"</span>);</span><br><span class="line">      student.setAge(<span class="number">23</span>);</span><br><span class="line">      List&lt;Student&gt; studentList = studentDao.select3(student);</span><br><span class="line">      <span class="keyword">for</span> (Student student1:studentList)&#123;</span><br><span class="line">          System.out.println(student1);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">	#&#123;&#125;中的属性名必须是domain类的属性名</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select3"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student where name=#&#123;name&#125; and name=#&#123;name&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="map"><a href="#map" class="headerlink" title="map"></a>map</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//4.测试：使用map为参数</span></span><br><span class="line">      Map&lt;String,Object&gt; map = <span class="keyword">new</span> HashMap&lt;String, Object&gt;();</span><br><span class="line">      map.put(<span class="string">"name"</span>,<span class="string">"wyf"</span>);</span><br><span class="line">      map.put(<span class="string">"age"</span>, <span class="number">23</span>);</span><br><span class="line">      List&lt;Student&gt; studentList = studentDao.select4(map);</span><br><span class="line">      <span class="keyword">for</span> (Student student1:studentList)&#123;</span><br><span class="line">          System.out.println(student1);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select4"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student where name=#&#123;name&#125; and name=#&#123;name&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="与"><a href="#与" class="headerlink" title="#{}与${}"></a>#{}与${}</h3><ul>
<li><p>#{}：表示占位符，可以有效防止SQL注入。使用其设置参数无需考虑参数的类型，只能占值的位置</p>
</li>
<li><p>${}：表示拼接符，无法防止SQL注入。使用其设置参数必须考虑参数的类型</p>
</li>
<li><p>传递简单参数类型</p>
<ul>
<li>如果获取简单类型参数，#{}中可以使用value或其他名称</li>
<li>如果获取简单参数类型，${}中只能使用value</li>
</ul>
</li>
<li><p>在没有特殊要求的情况下，通常使用#{}占位符</p>
</li>
<li><p>在某些情况下必须使用${}</p>
<ul>
<li><p>如：需要动态拼接表名</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Select * from $&#123;tablename&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>如：动态【拼接排序字段</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select * from tablename order by $&#123;username&#125; desc</span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ul>
<h3 id="like模糊查询"><a href="#like模糊查询" class="headerlink" title="like模糊查询"></a>like模糊查询</h3><h4 id="使用"><a href="#使用" class="headerlink" title="使用${}"></a>使用${}</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//5.测试：like模糊查询 方式1：使用 $&#123;&#125;了解</span></span><br><span class="line">      List&lt;Student&gt; studentList = studentDao.select5(<span class="string">"z"</span>);</span><br><span class="line">      <span class="keyword">for</span> (Student student:studentList)&#123;</span><br><span class="line">          System.out.println(student);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select5"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student where name like '%$&#123;value&#125;%'</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="使用-1"><a href="#使用-1" class="headerlink" title="使用#{}"></a>使用#{}</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//6.测试：like模糊查询 方式1：使用 $&#123;&#125;了解</span></span><br><span class="line">      List&lt;Student&gt; studentList = studentDao.select6(<span class="string">"z"</span>);</span><br><span class="line">      <span class="keyword">for</span> (Student student:studentList)&#123;</span><br><span class="line">          System.out.println(student);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">   <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">       %空格#&#123;&#125;空格%</span></span><br><span class="line"><span class="comment">	空格用于拼接，不能省略</span></span><br><span class="line"><span class="comment">   --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select6"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student where name like '%' #&#123;name&#125; '%'</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="resultType"><a href="#resultType" class="headerlink" title="resultType"></a>resultType</h3><p>设置返回值类型</p>
<ul>
<li>返回简单类型  8+String</li>
<li>返回pojo  domain</li>
<li>返回hashmap（注意返回值类型）</li>
<li>当查询字段名和pojo属性名不一致时的解决方案</li>
</ul>
<h4 id="返回String"><a href="#返回String" class="headerlink" title="返回String"></a>返回String</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//10.测试：返回String类型</span></span><br><span class="line">      String name = studentDao.select10(<span class="string">"A0001"</span>);</span><br><span class="line">      System.out.println(name);</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select10"</span> <span class="attr">resultType</span>=<span class="string">"String"</span>&gt;</span></span><br><span class="line">       select name from tbl_student where id=#&#123;id&#125;</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="返回String类型集合"><a href="#返回String类型集合" class="headerlink" title="返回String类型集合"></a>返回String类型集合</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//11.测试：返回String类型集合</span></span><br><span class="line">      <span class="comment">//查询出所有学生的名字</span></span><br><span class="line">      List&lt;String&gt; nameList= studentDao.select11();</span><br><span class="line">      <span class="keyword">for</span> (String name1:nameList)&#123;</span><br><span class="line">          System.out.println(name1);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select11"</span> <span class="attr">resultType</span>=<span class="string">"String"</span>&gt;</span></span><br><span class="line">       select name from tbl_student</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="返回int类型"><a href="#返回int类型" class="headerlink" title="返回int类型"></a>返回int类型</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//12.测试：返回int类型</span></span><br><span class="line">      <span class="comment">//需求：查询表中一共有多少条信息</span></span><br><span class="line">      <span class="keyword">int</span> count = studentDao.select12();</span><br><span class="line">      System.out.println(count);</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select12"</span> <span class="attr">resultType</span>=<span class="string">"int"</span>&gt;</span></span><br><span class="line">       select count(*) from tbl_student</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="返回domain类型（略）"><a href="#返回domain类型（略）" class="headerlink" title="返回domain类型（略）"></a>返回domain类型（略）</h4><h4 id="返回map类型"><a href="#返回map类型" class="headerlink" title="返回map类型"></a>返回map类型</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//14.测试：返回map类型</span></span><br><span class="line">      <span class="comment">/*</span></span><br><span class="line"><span class="comment">          系统会为我们自动创建出来一个list集合来保存这些map对象</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">      List&lt;Map&lt;String,Object&gt;&gt; mapList= studentDao.select14();</span><br><span class="line">      <span class="comment">//拆分list</span></span><br><span class="line">      <span class="keyword">for</span> (Map&lt;String,Object&gt; map:mapList)&#123;</span><br><span class="line">          Set&lt;String&gt; set = map.keySet();</span><br><span class="line">          <span class="keyword">for</span> (String key:set)&#123;</span><br><span class="line">              <span class="comment">// 获取key</span></span><br><span class="line">              System.out.print(key+<span class="string">"="</span>);</span><br><span class="line">              <span class="comment">// 获取value</span></span><br><span class="line">              System.out.println(map.get(key));</span><br><span class="line">          &#125;</span><br><span class="line">          System.out.println(<span class="string">"-----------------------------------"</span>);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select14"</span> <span class="attr">resultType</span>=<span class="string">"map"</span>&gt;</span></span><br><span class="line">       select * from tbl_student</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="domain和map"><a href="#domain和map" class="headerlink" title="domain和map"></a>domain和map</h4><ul>
<li>对于SQL语句查询的结果，使用domain会更加方便</li>
<li>但是对于查询结果，很多情况，使用domain封装不了，所以我们会想到使用map来保存结果，如：根据年龄分组，查询出来每一个姓名对应的数量</li>
</ul>
<h4 id="对于字段名和domain属性类型不一致的处理"><a href="#对于字段名和domain属性类型不一致的处理" class="headerlink" title="对于字段名和domain属性类型不一致的处理"></a>对于字段名和domain属性类型不一致的处理</h4><h5 id="起别名"><a href="#起别名" class="headerlink" title="起别名"></a>起别名</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//15.测试：当数据库表字段名称和domain类属性名不一致时的处理</span></span><br><span class="line">      List&lt;Student&gt; studentList = studentDao.select15();</span><br><span class="line">      <span class="keyword">for</span> (Student student:studentList)&#123;</span><br><span class="line">          System.out.println(student);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 起别名的方式--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select15"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select id,fullname as name,age from tbl_student</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h5 id="使用resultMap"><a href="#使用resultMap" class="headerlink" title="使用resultMap"></a>使用resultMap</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//16.测试：当数据库表字段名称和domain类属性名不一致时的处理</span></span><br><span class="line">      List&lt;Student&gt; studentList = studentDao.select16();</span><br><span class="line">      <span class="keyword">for</span> (Student student:studentList)&#123;</span><br><span class="line">          System.out.println(student);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">       id:唯一标识</span></span><br><span class="line"><span class="comment">           将来在使用到该resultMap标签时，使用id来找到这组标签</span></span><br><span class="line"><span class="comment">       type：指定一个类型，与数据库表一一对应，建立起表字段和类属性的名字一一匹配的关系</span></span><br><span class="line"><span class="comment">   --&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"stuMap"</span> <span class="attr">type</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">           id:用来配置主键的对应关系</span></span><br><span class="line"><span class="comment">           result：用来配置普通字段对应关系的</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">           对于tbl_student表，表结果时一个id，两个普通字段</span></span><br><span class="line"><span class="comment">           所以我们需要一个id标签，两个result标签</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">           property属性：配置的是类中的属性名</span></span><br><span class="line"><span class="comment">           column属性：配置的是表中的字段名</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">           这样就能建立起类属性和表字段一一对应的关系了</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">id</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"id"</span>/&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"fullname"</span>/&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">result</span> <span class="attr">property</span>=<span class="string">"age"</span> <span class="attr">column</span>=<span class="string">"age"</span>/&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">resultMap</span>&gt;</span></span><br><span class="line"></span><br><span class="line">   <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select16"</span> <span class="attr">resultMap</span>=<span class="string">"stuMap"</span>&gt;</span></span><br><span class="line">       select * from tbl_student</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="动态SQL"><a href="#动态SQL" class="headerlink" title="动态SQL"></a>动态SQL</h3><h4 id="where-if"><a href="#where-if" class="headerlink" title="where + if"></a>where + if</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//17.测试：动态SQL   where标签结合if标签</span></span><br><span class="line">      Student student = <span class="keyword">new</span> Student();</span><br><span class="line">      student.setName(<span class="string">"y"</span>);</span><br><span class="line">      student.setAddress(<span class="string">"a"</span>);</span><br><span class="line">      List&lt;Student&gt; studentList1 = studentDao.select17(student);</span><br><span class="line">      <span class="keyword">for</span> (Student student1:studentList1)&#123;</span><br><span class="line">          System.out.println(student1);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select17"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">        select * from tbl_student</span><br><span class="line">        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">            where标签：</span></span><br><span class="line"><span class="comment">                    当where标签在使用的时候，必须要搭配where标签对中的if标签来使用</span></span><br><span class="line"><span class="comment">                    通过if标签的判断，如果有查询条件，则展现where关键字，如果没有查询条件则不展现where关键字</span></span><br><span class="line"><span class="comment">					where标签会自动的屏蔽掉第一个连接符 and/or</span></span><br><span class="line"><span class="comment">        --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">where</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--name不等于空，同时name不等于空串--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"name!=null and name!=''"</span>&gt;</span></span><br><span class="line">                and name like '%' #&#123;name&#125; '%'</span><br><span class="line">            <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="tag">&lt;<span class="name">if</span> <span class="attr">test</span>=<span class="string">"address!=null and address!=''"</span>&gt;</span></span><br><span class="line">                and address like '%' #&#123;address&#125; '%'</span><br><span class="line">            <span class="tag">&lt;/<span class="name">if</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;/<span class="name">where</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="foreach"><a href="#foreach" class="headerlink" title="foreach"></a>foreach</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 18.测试；动态SQL foreach标签</span></span><br><span class="line">        <span class="comment">//查询这三个学员的信息</span></span><br><span class="line">        String stringArr[] = &#123;<span class="string">"A0001"</span>,<span class="string">"A0002"</span>,<span class="string">"A0003"</span>&#125;;</span><br><span class="line">        List&lt;Student&gt; studentList = studentDao.select18(stringArr);</span><br><span class="line">        <span class="keyword">for</span> (Student student1:studentList)&#123;</span><br><span class="line">            System.out.println(student1);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select18"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">       select * from tbl_student</span><br><span class="line">       where id in</span><br><span class="line">       <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">           foreach标签：用来遍历传递来的数组标签</span></span><br><span class="line"><span class="comment">               collection：标识传递参数的类型</span></span><br><span class="line"><span class="comment">                       array：数组</span></span><br><span class="line"><span class="comment">                       list：集合</span></span><br><span class="line"><span class="comment">               item：每一次遍历出来的元素，在使用该元素的时候，需要套用在#&#123;&#125;中</span></span><br><span class="line"><span class="comment">               open：拼接循环的开始符号</span></span><br><span class="line"><span class="comment">               close：拼接循环的结束符号</span></span><br><span class="line"><span class="comment">               separator：元素与元素之间的分隔符</span></span><br><span class="line"><span class="comment">       --&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">"array"</span> <span class="attr">item</span>=<span class="string">"id"</span> <span class="attr">open</span>=<span class="string">"("</span> <span class="attr">close</span>=<span class="string">")"</span> <span class="attr">separator</span>=<span class="string">","</span>&gt;</span></span><br><span class="line">           #&#123;id&#125;</span><br><span class="line">       <span class="tag">&lt;/<span class="name">foreach</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="SQL片段"><a href="#SQL片段" class="headerlink" title="SQL片段"></a>SQL片段</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//19.测试：sql片段</span></span><br><span class="line">      Student student = studentDao.select19(<span class="string">"A0001"</span>);</span><br><span class="line">      System.out.println(student);</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">    使用SQL标签制作SQL片段</span></span><br><span class="line"><span class="comment">    SQL片段的作用是用来代替SQL语句中的代码</span></span><br><span class="line"><span class="comment">    如果你的mapper映射文件中的SQL语句某些代码出现了大量的重复，我们可以使用SQL片段来代替他们</span></span><br><span class="line"><span class="comment">    id：SQL片段的唯一标识，将来找到SQL片段使用id来进行定位</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    将来的实际项目开发中，使用SQL片段来替代重复率高，且复杂的子查询</span></span><br><span class="line"><span class="comment">    如：嵌套查询</span></span><br><span class="line"><span class="comment">    注意：对于SQL片段</span></span><br><span class="line"><span class="comment">            在同一个mapper下，大量的出现重复的子查询的几率不高，所以一般情况下没有使用SQL片段的必要</span></span><br><span class="line"><span class="comment">            在实际项目开发中，如果大量的使用SQL片段，会大大的降低SQL语句的可读性</span></span><br><span class="line"><span class="comment">            在很多企业中，干脆不用</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"sql1"</span>&gt;</span></span><br><span class="line">    select * from tbl_student</span><br><span class="line"><span class="tag">&lt;/<span class="name">sql</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select19"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.domain.Student"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"sql1"</span>/&gt;</span> where id=#&#123;id&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="多表联查"><a href="#多表联查" class="headerlink" title="多表联查"></a>多表联查</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//20.测试：多表联查</span></span><br><span class="line">      <span class="comment">//查询出学生姓名和班级</span></span><br><span class="line">      List&lt;Map&lt;String,Object&gt;&gt; mapListList = studentDao.select20();</span><br><span class="line">      <span class="keyword">for</span> (Map&lt;String,Object&gt; map:mapListList)&#123;</span><br><span class="line">          Set&lt;String&gt; set = map.keySet();</span><br><span class="line">          <span class="keyword">for</span> (String key:set)&#123;</span><br><span class="line">              <span class="comment">// 获取key</span></span><br><span class="line">              System.out.print(key+<span class="string">"="</span>);</span><br><span class="line">              <span class="comment">// 获取value</span></span><br><span class="line">              System.out.println(map.get(key));</span><br><span class="line">          &#125;</span><br><span class="line">          System.out.println(<span class="string">"-------------------------"</span>);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select20"</span> <span class="attr">resultType</span>=<span class="string">"map"</span>&gt;</span></span><br><span class="line">       select s.name as 学生,c.name as 班级</span><br><span class="line">       from tbl_student s</span><br><span class="line">       join tbl_classroom c</span><br><span class="line">       on s.classroomId=c.id</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="使用vo"><a href="#使用vo" class="headerlink" title="使用vo"></a>使用vo</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//21.测试：多表联查</span></span><br><span class="line">        <span class="comment">//查询出学生和班级所有信息，加VO（value Object）</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *   在实际项目开发中，如果需要为前端展现数据，使用一个domain类型不足以表现出来这些数据</span></span><br><span class="line"><span class="comment">        *   这时我们可以考虑使用两种技术来实现</span></span><br><span class="line"><span class="comment">        *   分别为：</span></span><br><span class="line"><span class="comment">        *       使用map</span></span><br><span class="line"><span class="comment">        *       使用vo</span></span><br><span class="line"><span class="comment">        *   例如我们现在的需求</span></span><br><span class="line"><span class="comment">        *       查询出学生和班级所有信息</span></span><br><span class="line"><span class="comment">        *       得到的结果 使用学生的domain或者班级的domain都不能都封装这些结果</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        *       所以我们可以使用map去保存这些信息</span></span><br><span class="line"><span class="comment">        *       同时我们也可以使用vo类来保存这些信息</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        *       vo值得是创建出来一个类，这个类中的属性是完全由我们自己去定义，属性会保存所有需要展现的信息</span></span><br><span class="line"><span class="comment">        *       例如我们现在这个例子，我们可以使用vo来封装所有与学生和班级相关的信息</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        *       vo</span></span><br><span class="line"><span class="comment">        *           student</span></span><br><span class="line"><span class="comment">        *           classroom</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        List&lt;StudentAndClassroomVo&gt; studentAndClassroomVoList = studentDao.select21();</span><br><span class="line">        <span class="keyword">for</span> (StudentAndClassroomVo studentAndClassroomVo:studentAndClassroomVoList)&#123;</span><br><span class="line">            System.out.println(studentAndClassroomVo);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select21"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.vo.StudentAndClassroomVo"</span>&gt;</span></span><br><span class="line">        select</span><br><span class="line">        s.id sid,</span><br><span class="line">        s.name sname,</span><br><span class="line">        s.age sage,</span><br><span class="line">        s.address saddress,</span><br><span class="line">        c.id cid,</span><br><span class="line">        c.name cname</span><br><span class="line">        from tbl_student s</span><br><span class="line">        join tbl_classroom c</span><br><span class="line">        on s.classroomId=c.id</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="vo条件查询"><a href="#vo条件查询" class="headerlink" title="vo条件查询"></a>vo条件查询</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//22.测试：多表联查</span></span><br><span class="line">      <span class="comment">//查询出带有字母z的学生和班级所有信息</span></span><br><span class="line">      List&lt;StudentAndClassroomVo&gt; studentAndClassroomVoList = studentDao.select22(<span class="string">"z"</span>);</span><br><span class="line">      <span class="keyword">for</span> (StudentAndClassroomVo studentAndClassroomVo:studentAndClassroomVoList)&#123;</span><br><span class="line">          System.out.println(studentAndClassroomVo);</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"select22"</span> <span class="attr">resultType</span>=<span class="string">"com.lmt.vo.StudentAndClassroomVo"</span>&gt;</span></span><br><span class="line">       select</span><br><span class="line">       s.id sid,</span><br><span class="line">       s.name sname,</span><br><span class="line">       s.age sage,</span><br><span class="line">       s.address saddress,</span><br><span class="line">       c.id cid,</span><br><span class="line">       c.name cname</span><br><span class="line"></span><br><span class="line">       from tbl_student s</span><br><span class="line">       join tbl_classroom c</span><br><span class="line">       on s.classroomId=c.id</span><br><span class="line"></span><br><span class="line">       where s.name like '%' #&#123;name&#125; '%'</span><br><span class="line">   <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="map-与-vo"><a href="#map-与-vo" class="headerlink" title="map 与 vo"></a>map 与 vo</h4><ul>
<li>如果前端的需求的重复率不高，那么我们选择使用map就可以了</li>
<li>如果前端对于该需求的重复率较高，那么我们就可以创建一个vo类使用</li>
</ul>
<h3 id="使用注解"><a href="#使用注解" class="headerlink" title="使用注解"></a>使用注解</h3><ul>
<li><p>注解在接口上实现</p>
</li>
<li><p>需要在核心配置文件中绑定接口</p>
</li>
<li><p>本质：反射机制实现</p>
</li>
<li><p>底层：动态代理</p>
</li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>呐呐呐，赏点吧</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="孤失狐 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="孤失狐 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/11/10/IDEA%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7/" rel="next" title="IDEA使用技巧">
                <i class="fa fa-chevron-left"></i> IDEA使用技巧
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/11/10/opencv/" rel="prev" title="opencv">
                opencv <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/ChatHead.jpg"
                alt="孤失狐" />
            
              <p class="site-author-name" itemprop="name">孤失狐</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%7C%7C%20archive">
              
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/gushihu1" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://space.bilibili.com/183711782" target="_blank" title="Bilibili">
                      
                        <i class="fa fa-fw fa-bilibili"></i>Bilibili</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://yangbin1215.gitee.io/" title="杨彬的码云博客" target="_blank">杨彬的码云博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yangbin1215.github.io/" title="杨彬的GitHub博客" target="_blank">杨彬的GitHub博客</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Mybatis"><span class="nav-number">1.</span> <span class="nav-text">Mybatis</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#jar包依赖"><span class="nav-number">1.0.1.</span> <span class="nav-text">jar包依赖</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#配置mapper文件夹为配置文件夹"><span class="nav-number">1.0.1.1.</span> <span class="nav-text">配置mapper文件夹为配置文件夹</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置文件"><span class="nav-number">1.0.2.</span> <span class="nav-text">配置文件</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#主配置文件"><span class="nav-number">1.0.2.1.</span> <span class="nav-text">主配置文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#mapper配置文件"><span class="nav-number">1.0.2.2.</span> <span class="nav-text">mapper配置文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#log4j配置信息"><span class="nav-number">1.0.2.3.</span> <span class="nav-text">log4j配置信息</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#连接"><span class="nav-number">1.0.3.</span> <span class="nav-text">连接</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建"><span class="nav-number">1.0.3.1.</span> <span class="nav-text">创建</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#关闭"><span class="nav-number">1.0.3.2.</span> <span class="nav-text">关闭</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#操作"><span class="nav-number">1.0.4.</span> <span class="nav-text">操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#查单条"><span class="nav-number">1.0.4.1.</span> <span class="nav-text">查单条</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查所有"><span class="nav-number">1.0.4.2.</span> <span class="nav-text">查所有</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#添加"><span class="nav-number">1.0.4.3.</span> <span class="nav-text">添加</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改"><span class="nav-number">1.0.4.4.</span> <span class="nav-text">修改</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除"><span class="nav-number">1.0.4.5.</span> <span class="nav-text">删除</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Mybatis与JDBC"><span class="nav-number">1.0.5.</span> <span class="nav-text">Mybatis与JDBC</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Util工具类"><span class="nav-number">1.0.6.</span> <span class="nav-text">Util工具类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#业务层动态代理类"><span class="nav-number">1.0.6.1.</span> <span class="nav-text">业务层动态代理类</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#通过业务层实现"><span class="nav-number">1.0.7.</span> <span class="nav-text">通过业务层实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Mybatis对dao层动态代理的支持"><span class="nav-number">1.0.7.1.</span> <span class="nav-text">Mybatis对dao层动态代理的支持</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#规定"><span class="nav-number">1.0.7.2.</span> <span class="nav-text">规定</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#增删改查"><span class="nav-number">1.0.8.</span> <span class="nav-text">增删改查</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#查询所有"><span class="nav-number">1.0.8.1.</span> <span class="nav-text">查询所有</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置文件配置"><span class="nav-number">1.0.9.</span> <span class="nav-text">配置文件配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#主配置文件-1"><span class="nav-number">1.0.9.1.</span> <span class="nav-text">主配置文件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#海量级别的数据，提高效率"><span class="nav-number">1.0.10.</span> <span class="nav-text">海量级别的数据，提高效率</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#基础操作"><span class="nav-number">1.0.10.1.</span> <span class="nav-text">基础操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#高级操作"><span class="nav-number">1.0.10.2.</span> <span class="nav-text">高级操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#专业操作"><span class="nav-number">1.0.10.3.</span> <span class="nav-text">专业操作</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#简单类型查询"><span class="nav-number">1.0.11.</span> <span class="nav-text">简单类型查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#String"><span class="nav-number">1.0.11.1.</span> <span class="nav-text">String</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#int"><span class="nav-number">1.0.11.2.</span> <span class="nav-text">int</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引用类型和集合类型"><span class="nav-number">1.0.12.</span> <span class="nav-text">引用类型和集合类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#domain"><span class="nav-number">1.0.12.1.</span> <span class="nav-text">domain</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map"><span class="nav-number">1.0.12.2.</span> <span class="nav-text">map</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#与"><span class="nav-number">1.0.13.</span> <span class="nav-text">#{}与${}</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#like模糊查询"><span class="nav-number">1.0.14.</span> <span class="nav-text">like模糊查询</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#使用"><span class="nav-number">1.0.14.1.</span> <span class="nav-text">使用${}</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#使用-1"><span class="nav-number">1.0.14.2.</span> <span class="nav-text">使用#{}</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#resultType"><span class="nav-number">1.0.15.</span> <span class="nav-text">resultType</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#返回String"><span class="nav-number">1.0.15.1.</span> <span class="nav-text">返回String</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回String类型集合"><span class="nav-number">1.0.15.2.</span> <span class="nav-text">返回String类型集合</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回int类型"><span class="nav-number">1.0.15.3.</span> <span class="nav-text">返回int类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回domain类型（略）"><span class="nav-number">1.0.15.4.</span> <span class="nav-text">返回domain类型（略）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#返回map类型"><span class="nav-number">1.0.15.5.</span> <span class="nav-text">返回map类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#domain和map"><span class="nav-number">1.0.15.6.</span> <span class="nav-text">domain和map</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#对于字段名和domain属性类型不一致的处理"><span class="nav-number">1.0.15.7.</span> <span class="nav-text">对于字段名和domain属性类型不一致的处理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#起别名"><span class="nav-number">1.0.15.7.1.</span> <span class="nav-text">起别名</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#使用resultMap"><span class="nav-number">1.0.15.7.2.</span> <span class="nav-text">使用resultMap</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动态SQL"><span class="nav-number">1.0.16.</span> <span class="nav-text">动态SQL</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#where-if"><span class="nav-number">1.0.16.1.</span> <span class="nav-text">where + if</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#foreach"><span class="nav-number">1.0.16.2.</span> <span class="nav-text">foreach</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SQL片段"><span class="nav-number">1.0.17.</span> <span class="nav-text">SQL片段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多表联查"><span class="nav-number">1.0.18.</span> <span class="nav-text">多表联查</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#使用vo"><span class="nav-number">1.0.18.1.</span> <span class="nav-text">使用vo</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vo条件查询"><span class="nav-number">1.0.18.2.</span> <span class="nav-text">vo条件查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map-与-vo"><span class="nav-number">1.0.18.3.</span> <span class="nav-text">map 与 vo</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用注解"><span class="nav-number">1.0.19.</span> <span class="nav-text">使用注解</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">孤失狐</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":1},"log":false});</script></body>
</html>
